package com.dazhewan.project.ocr.strategy;

import com.baidu.aip.ocr.AipOcr;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author zhengxuexue
 * @date 2023/12/15 16:32
 * @description 基于百度OCR接口的识别策略
 */
public class BaiDuOcrStrategy implements IOrcStrategyI {
    public static final String APP_ID = "25593566";
    public static final String API_KEY = "bcCNXZyj05Es5AUgPxUvx86A";
    public static final String SECRET_KEY = "6MugNYL1KuueHmS4fyOYWzwIOrj8uGpD";
    private final static BaiDuOcrStrategy ocr=new BaiDuOcrStrategy();

    private BaiDuOcrStrategy() {
    }


    public static IOrcStrategyI getInstance() {
        return ocr;
    }

    @Override
    public String identifyWordsByImage(BufferedImage bufferedImage) throws IOException {
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        // 传入可选参数调用接口
        HashMap options = new HashMap();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "png", os);
        // 参数为二进制数组
        final org.json.JSONObject jsonObject = client.basicGeneral(os.toByteArray(), options);
        return "";
    }
}

